const UserModel = require("../../models/UserModel");

const UserService = {
  login: async (req, res) => {
    const { username, password } = req.body;
    const result = await UserModel.find({
      username,
      password,
    });
    return result;
  },
  upload: async (file, id) => {
    const filePath = file.path.replace("public", "");
    const res = await UserModel.updateOne(
      { _id: id },
      { password: 123456, avata: filePath }
    );
    return filePath;
  },

  addUser: async (req, res) => {
    const { name: username, password } = req.body;
    return await UserModel.create({ username, password });
  },

  getInfo: async (id) => {
    return await UserModel.findOne({ _id: id });
  },

  edit: async (id, body, file) => {
    const filePath = file.path.replace("public", "");
    console.log(body);
    return await UserModel.updateOne(
      {
        _id: id,
      },
      { ...body, avata: filePath }
    );
  },

  delete: async (id) => {
    return await UserModel.deleteOne({ _id: id });
  },

  getAllUser: async (req, res) => {
    return await UserModel.find({});
  },
};

module.exports = UserService;
